﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace RuoVea.ExSugar;

/// <summary>
/// IServiceCollection Extensions
/// </summary>
public static partial class ServiceExtensions {
    /// <summary>
    /// AddDbContext
    /// </summary>
    /// <typeparam name="serviceType"></typeparam>
    /// <param name="services"></param>
    /// <param name="implementationFactory"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static IServiceCollection AddDbContext<serviceType>(this IServiceCollection services, Func<IServiceProvider, serviceType> implementationFactory)
       where serviceType : DbContext
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }
        if (implementationFactory == null)
        {
            throw new ArgumentNullException(nameof(implementationFactory));
        }
        services.AddScoped<serviceType>(implementationFactory);
        return services;
    }
}

